<template>
  <el-dialog width="1000" title="活动列表" v-model="visible" destroy-on-close draggable>
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="礼包名称" prop="packName">
        <el-input
          maxlength="16"
          v-model.trim="queryParams.packName"
          placeholder="请输入礼包名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <!-- <el-table-column label="" align="center" prop="" width="50">

         </el-table-column> -->
      <el-table-column label="礼包名称" align="center" prop="packName" />
      <el-table-column label="礼包内容" align="center" prop="couponCount" />
      <el-table-column label="已领取" align="center" prop="receiveCount" />
      <el-table-column label="剩余" align="center" prop="surplusCount" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="{ row }">
          <el-button :type="row.status == 1 ? 'success' : ''" link>
            {{ row.status === 1 ? '进行中' : '已失效' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList()"
    />
    <div class="confirmedBox">
      <el-button @click="confirmed()">保存</el-button>
      <el-button @click="visible = false">取消</el-button>
    </div>
  </el-dialog>
</template>
<script setup>
import { getCouponPackListApi } from '@/api/marketing/giftPack'

const visible = ref(false)
const loading = ref(false)
const total = ref(0)
const tableList = ref([])
const queryRef = ref()
const queryParams = ref({
  packName: null,
  status: 1,
  pageNum: 1,
  pageSize: 10,
  gtSurplusCount: 0
})

function init(cityName) {
  visible.value = true
  queryParams.value.cityName = cityName
  getList()
}
function getList() {
  loading.value = true
  getCouponPackListApi(queryParams.value)
    .then((res) => {
      loading.value = false
      tableList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
    })
}
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
function resetQuery() {
  // queryParams.value.packName = null
  const cityName = queryParams.value.cityName
  queryRef.value.resetFields()
  queryParams.value.cityName = cityName
  handleQuery()
}
const giftBagInfo = ref()
function handleCurrentChange(row) {
  giftBagInfo.value = row
}
function confirmed() {
  emits('passingValue', giftBagInfo.value)
  visible.value = false
}
defineExpose({
  init
})
const emits = defineEmits(['passingValue'])
</script>
<style lang="scss" scoped>
:deep(.el-pagination) {
  position: relative;
}
.confirmedBox {
  display: flex;
  justify-content: end;
  margin-top: 50px;
}
</style>
